﻿@{
    if(!Check.CheckUrl() || !Check.CheckLogin(false))
    {
        Response.Write("[]");
        Response.End();
    }
    
    string rootid = Request.QueryString["rootid"];
    string showtype = Request.QueryString["showtype"];
    Business.Platform.Organize BOrganize = new Business.Platform.Organize();
    System.Text.StringBuilder json = new System.Text.StringBuilder("[", 1000);

    if ("1" == showtype)//显示工作组
    {
        Business.Platform.WorkGroup BWorkGroup = new Business.Platform.WorkGroup();
        var workGroups = BWorkGroup.GetAll();
        
        json.Append("{");
        json.AppendFormat("\"id\":\"{0}\",", Guid.Empty);
        json.AppendFormat("\"parentID\":\"{0}\",", Guid.Empty);
        json.AppendFormat("\"title\":\"{0}\",", "工作组");
        json.AppendFormat("\"ico\":\"{0}\",", "/images/ico/group.gif");
        json.AppendFormat("\"link\":\"{0}\",", "");
        json.AppendFormat("\"type\":\"{0}\",", 5);
        json.AppendFormat("\"hasChilds\":\"{0}\",", workGroups.Count);
        json.Append("\"childs\":[");

        int countwg = workGroups.Count;
        int iwg = 0;
        foreach(var wg in workGroups)
        {
            json.Append("{");
            json.AppendFormat("\"id\":\"{0}\",", wg.ID);
            json.AppendFormat("\"parentID\":\"{0}\",", Guid.Empty);
            json.AppendFormat("\"title\":\"{0}\",", wg.Name);
            json.AppendFormat("\"ico\":\"{0}\",", "");
            json.AppendFormat("\"link\":\"{0}\",", "");
            json.AppendFormat("\"type\":\"{0}\",", 5);
            json.AppendFormat("\"hasChilds\":\"{0}\",", 0);
            json.Append("\"childs\":[");
            json.Append("]");
            json.Append("}");
            if (iwg++ < countwg - 1)
            {
                json.Append(",");
            }
        }
        
        json.Append("]");
        json.Append("}");
        json.Append("]");
        Response.Write(json.ToString());
        Response.End();
    }

    
    Guid rootID;
    Data.Model.Organize root;
    if(rootid.IsGuid(out rootID))
    {
        root = BOrganize.Get(rootID);
    }
    else
    {
        root = BOrganize.GetRoot();
    }

    List<Data.Model.Users> users = new List<Data.Model.Users>();
    
    Business.Platform.Users busers = new Business.Platform.Users();
    users = busers.GetAllByOrganizeID(root.ID);
    
  
    json.Append("{");
    json.AppendFormat("\"id\":\"{0}\",", root.ID);
    json.AppendFormat("\"parentID\":\"{0}\",", root.ParentID);
    json.AppendFormat("\"title\":\"{0}\",", root.Name);
    json.AppendFormat("\"ico\":\"{0}\",", "/images/ico/icon_site.gif");
    json.AppendFormat("\"link\":\"{0}\",", "");
    json.AppendFormat("\"type\":\"{0}\",", root.Type);
    json.AppendFormat("\"hasChilds\":\"{0}\",", root.ChildsLength == 0 && users.Count == 0 ? "0" : "1");
    json.Append("\"childs\":[");

    var orgs =  BOrganize.GetChilds(root.ID);
    int count = orgs.Count;
    
    int i = 0;
    foreach (var org in orgs)
    {
        json.Append("{");
        json.AppendFormat("\"id\":\"{0}\",", org.ID);
        json.AppendFormat("\"parentID\":\"{0}\",", org.ParentID);
        json.AppendFormat("\"title\":\"{0}\",", org.Name);
        json.AppendFormat("\"ico\":\"{0}\",", "");
        json.AppendFormat("\"link\":\"{0}\",", "");
        json.AppendFormat("\"type\":\"{0}\",", org.Type);
        json.AppendFormat("\"hasChilds\":\"{0}\",", org.ChildsLength);
        json.Append("\"childs\":[");
        json.Append("]");
        json.Append("}");
        if (i++ < count - 1 || users.Count>0)
        {
            json.Append(",");
        }
    }

    if(users.Count>0)
    {
        var userRelations = new Business.Platform.UsersRelation().GetAllByOrganizeID(root.ID);
        int count1 = users.Count;
        int j = 0;
        foreach (var user in users)
        {
            var ur = userRelations.Find(p => p.UserID == user.ID);
            json.Append("{");
            json.AppendFormat("\"id\":\"{0}\",", user.ID);
            json.AppendFormat("\"parentID\":\"{0}\",", root.ID);
            json.AppendFormat("\"title\":\"{0}{1}\",", user.Name, ur != null && ur.IsMain == 0 ? "<span style='color:#999;'>[兼职]</span>" : "");
            json.AppendFormat("\"ico\":\"{0}\",", "");
            json.AppendFormat("\"link\":\"{0}\",", "");
            json.AppendFormat("\"type\":\"{0}\",", "4");
            json.AppendFormat("\"hasChilds\":\"{0}\",", "0");
            json.Append("\"childs\":[");
            json.Append("]");
            json.Append("}");
            if (j++ < count1 - 1)
            {
                json.Append(",");
            }
        }
    }
   

    json.Append("]");
    json.Append("}");
    json.Append("]");

    Response.Write(json.ToString());
}